package service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

/** 
* @author 朱宏亮
* @version 创建时间：2017年12月4日 下午4:52:47 
* 类说明 
*/
@Service
public class ProducerService {

    @Resource(name="jmsTemplate")
    private JmsTemplate jmsTemplate;
       
    @Resource(name="jmsTemplate2")
    private JmsTemplate jmsTemplate2;
      /**
       * 向指定队列发送消息
       */
      public void sendMessage(Destination destination, final String msg) {
        System.out.println("------------向队列" + destination.toString() + "发送了消息:" + msg);
        jmsTemplate.send(destination, new MessageCreator() {
          public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage(msg);
          }
        });
        System.out.println("------------向队列" + destination.toString() + "发送消息结束------------" );
      }
     

      
      /**
       * 发送订阅消息
       */
        public void sendTopicMessage(Destination destination, final String msg) {
            System.out.println("------------向Topic" + destination.toString() + "发送了消息:" + msg);
            jmsTemplate2.send(destination, new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                  return session.createTextMessage(msg);
                }
            });
            System.out.println("------------向Topic" + destination.toString() + "发送消息结束------------" );
        }
      
      
      
}
 